app = @bhv

app.directive('cityMap', ['$interval', '$timeout', ($interval, $timeout)->
  restrict: 'E'
  replace: true
  scope: {}
  template: "<div id='map'></div>"
  controller: ->
    @random = random = (min, max)->
      Math.floor(Math.random() * (max - min + 1)) + min;

    cityList = (->
      city for city of CITYMAP
    )()

    randomCity = ->
      length = cityList.length - 1
      cityList[random(0, length)]

    @getRomdomCityQueue = (length = 1)->
      queue = []
      for i in [0..length]
        from = ['北京', '广州', '上海', '长沙', '南京'][random(0, 4)] #ctrl.randomCity()
        to = randomCity()
        continue if from is to
        geoCoord = {}
        geoCoord[from] = CITYMAP[from]
        geoCoord[to] = CITYMAP[to]
        queue.push([
          {name:from, geoCoord:CITYMAP[from]},
          {name:to, value: random(0, 100), geoCoord:CITYMAP[to]}
        ])

      queue

  link: ($scope, element, attrs, ctrl)->
    mapChart = new ChinaMap($(element)[0])

    drawPoint = (cityList)->
      queue = []
      queue.push city[1] for city in cityList
      $timeout(->
        mapChart.chart.addMarkPoint(0, {data: queue})
      , (15 + Math.random() * 10) * 100)

    drawLine = ->
      city = ctrl.getRomdomCityQueue(ctrl.random(1, 5))

      mapChart.chart.addMarkLine(0, {
          data: city
      })
      drawPoint(city)

    #drawLine()

    $interval(->
      drawLine()
    ,3000)

#    $interval(->
#      mapChart.chart.refresh()
#      console.log 1
#    , 3 * 1000
#    )

])

app.directive('areaTop5', [->
  restrict: 'E'
  replace: true
  scope: {}
  templateUrl: "views/map/top5.html"
  link: ($scope, element, attrs)->
    $scope.data = [
      {name: "湖南机房", value: 424564.11, precent: 80}
      {name: "北京机房", value: 234564.11, precent: 50}
      {name: "天津机房", value: 224445.11, precent: 40}
      {name: "河北机房", value: 124564.11, precent: 30}
      {name: "广东机房", value: 124564.11, precent: 30}
    ]
])

app.directive('realtimeTable', ['$interval', ($interval)->
  restrict: 'E'
  replace: true
  scope: {}
  templateUrl: "views/map/realtime-table.html"
  link: ($scope, element, attrs)->

    random = (min, max)->
      Math.floor(Math.random() * (max - min + 1)) + min;

    $scope.colorList = ['#831618', '#827910', '#226b85']

    randomData = ()->
      play_time: random(2000, 8000)
      cache_time: random(30, 80)
      area:['湖南', '广州', '拉萨', '上海', '北京'][random(0, 4)]
      ip: "#{random(11, 201)}.#{random(11, 254)}.*.*"

    $scope.data = [randomData(), randomData(), randomData()]

    $interval(->
      $scope.data.shift()
      $scope.data.push(randomData())

    , 1000)

])